package com.gy.education.mine;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gy.education.BaseActivity;
import com.gy.education.MainActivity;
import com.gy.education.R;
import com.gy.education.utils.SelectPopupListener;
import com.gy.education.utils.SelectPopupUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：gy on 16/7/12
 * 说明：注册
 */
public class RegisterActivity extends BaseActivity {

    private EditText etPhone, etPwd,etClass;
    private TextView tvCode,tvSchool,tvCollege,tvMajor,tvYear;
    private LinearLayout llMore;
    private int time = 60;
    private String strPhone, strPwd;
    private SelectPopupUtil selectPopupUtil;
    private List<List<String>> selectDatas;

    @Override
    protected void initView() {
        setContentViewRes(R.layout.activity_register);
        setTitleMsg("注册");
        _baseRight_text.setText("验证");
        etPhone = (EditText) findViewById(R.id.activity_login_et_phone);
        etPwd = (EditText) findViewById(R.id.activity_login_et_pwd);
        etClass = (EditText) findViewById(R.id.register_et_class);
        tvCode = (TextView) findViewById(R.id.activity_login_tv_code);
        tvSchool = (TextView) findViewById(R.id.register_tv_school);
        tvCollege = (TextView) findViewById(R.id.register_tv_college);
        tvMajor = (TextView) findViewById(R.id.register_tv_major);
        tvYear = (TextView) findViewById(R.id.register_tv_year);
        llMore = (LinearLayout) findViewById(R.id.activity_register_ll_more);
    }

    @Override
    protected void initData() {
        selectPopupUtil = new SelectPopupUtil(this);
        selectDatas = new ArrayList<>();
    }

    @Override
    protected void setListener() {
        super.setListener();
        tvCode.setOnClickListener(this);
        tvSchool.setOnClickListener(this);
        tvCollege.setOnClickListener(this);
        tvMajor.setOnClickListener(this);
        tvYear.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id._right:
                if("验证".equals(_baseRight_text.getText().toString())){
                    strPhone = etPhone.getText().toString().trim();
                    strPwd = etPwd.getText().toString().trim();
                    if (TextUtils.isEmpty(strPhone)) {
                        showToast("请输入手机号");
                    } else if (strPhone.length() != 11) {
                        showToast("请输入正确的手机号");
                    } else if (TextUtils.isEmpty(strPwd)) {
                        showToast("请输入密码");
                    } else if (strPwd.length() != 6) {
                        showToast("密码长度为6位");
                    } else {
                        _baseRight_text.setText("完成");
                        llMore.setVisibility(View.VISIBLE);
                    }
                } else if("完成".equals(_baseRight_text.getText().toString())){
                    Intent intent = new Intent(this, MainActivity.class);
                    startActivity(intent);
                    setResult(101);
                    finish();
                }
                break;
            case R.id.activity_login_tv_code:
                if("获取验证码".equals(tvCode.getText().toString())){
                    timeHandler.sendEmptyMessageDelayed(0,0);
                }
                break;
            case R.id.register_tv_school://学校
                final List<String> schools = new ArrayList<>();
                schools.add("天津大学");
                schools.add("厦门大学");
                schools.add("天津城建大学");
                selectDatas.clear();
                selectDatas.add(schools);
                selectPopupUtil.selectPopup(this.getWindow().getDecorView(), 1, selectDatas,
                        new SelectPopupListener() {
                            @Override
                            public void selectPopup(List<Integer> list) {
                                int select = list.get(0);
                                tvCollege.setText(schools.get(select));
                            }
                        });
                break;
            case R.id.register_tv_college://学院
                final List<String> collegs = new ArrayList<>();
                collegs.add("第一学院");
                collegs.add("第二学院");
                collegs.add("第三学院");
                collegs.add("第四学院");
                collegs.add("第五学院");
                selectDatas.clear();
                selectDatas.add(collegs);
                selectPopupUtil.selectPopup(this.getWindow().getDecorView(), 1, selectDatas,
                        new SelectPopupListener() {
                            @Override
                            public void selectPopup(List<Integer> list) {
                                int select = list.get(0);
                                tvCollege.setText(collegs.get(select));
                            }
                        });
                break;
            case R.id.register_tv_major://专业
                final List<String> majors = new ArrayList<>();
                majors.add("第一专业");
                majors.add("第二专业");
                majors.add("第三专业");
                majors.add("第四专业");
                majors.add("第五专业");
                selectDatas.clear();
                selectDatas.add(majors);
                selectPopupUtil.selectPopup(this.getWindow().getDecorView(), 1, selectDatas,
                        new SelectPopupListener() {
                            @Override
                            public void selectPopup(List<Integer> list) {
                                int select = list.get(0);
                                tvMajor.setText(majors.get(select));
                            }
                        });
                break;
            case R.id.register_tv_year://年级
                final List<String> years = new ArrayList<>();
                for (int i = 0; i < 17; i++) {
                    years.add((2000 + i)+"年");
                }
                selectDatas.clear();
                selectDatas.add(years);
                selectPopupUtil.selectPopup(this.getWindow().getDecorView(), 1, selectDatas,
                        new SelectPopupListener() {
                            @Override
                            public void selectPopup(List<Integer> list) {
                                int select = list.get(0);
                                tvYear.setText(years.get(select));
                            }
                        });
                break;
        }
    }

    private Handler timeHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(time > 0){
                time--;
                tvCode.setText(time+"秒后可重新获取");
                timeHandler.sendEmptyMessageDelayed(0,1000);
            } else {
                time = 60;
                tvCode.setText("获取验证码");
            }
        }
    };
}
